<?php
class TagController extends Controller{
	public function actionIndex(){
		$tagName = NC::_g('tag','');
		if(!$tagName)
			throw new CHttpException('404','找不到该标签');
		$tags = Tags::model()->findByAttributes(array('tag_name'=>$tagName));
		$tagNewsId =array();
		if($tags)
			$tagNewsId = Tags::jsonToArray($tags->news_ids);
		else
			throw new CHttpException('404','该标签可能已经被移除！');
		$newsIds = $tagNewsId['news_ids'];
		$newsIds = implode(',',$newsIds);
		if(strpos($newsIds,',')){
			$newsInfo = new CActiveDataProvider('News',array(
				'criteria'=>array(
					'condition'=>"news_id in($newsIds)",
					'order'=>'post_time DESC',
				),
			));
		}else{
			$newsInfo = new CActiveDataProvider('News',array(
			'criteria'=>array(
					'condition'=>"news_id =$newsIds",
					'order'=>'post_time DESC',
				),
			));
		}
		//区别是用户标签还是搜索标签
		$user = array();
		$type ='search';
		$userId = NC::_g('id',0);
		if($userId){//用户标签
			$type = 'user';
			$this->menu=array(
					array('label'=>'发现', 'url'=>array('/news'),'linkOptions'=>array('class'=>'turn2'),'itemOptions'=>array('class'=>'nav_left')),
					array('label'=>'赞友', 'url'=>array('/upper'),'linkOptions'=>array('class'=>'turn1'),'itemOptions'=>array('class'=>'nav_mid')),
					array('label'=>'收藏', 'url'=>array('/favorites'),'linkOptions'=>array('class'=>'turn2'),'itemOptions'=>array('class'=>'nav_right')),
				);
			$user = User::model()->findByPk($userId);
			$this->render('user',array('newsDataProvider'=>$newsInfo,'tagName'=>$tagName,'user'=>$user,'type'=>$type));
		}else{
			if(!Yii::app()->user->isGuest){
				$user = User::model()->findByPk(Yii::app()->user->id);
			}
			$this->render('index',array('newsDataProvider'=>$newsInfo,'tagName'=>$tagName,'user'=>$user,'type'=>$type));
		}
	}
}